/**
 * 
 */
package com.cnhaimei.net;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Arrays;

import android.util.Log;

/**
 * @author Jin001.Wu@gmail.com <br>
 * @date 2011-7-5
 * 
 */
public abstract class TCPSocket extends NetSocket implements  Runnable {
	public static final int STATE_START   = 1;
	public static final int STATE_CONNETTING = 2;
	public static final int STATE_STOP    = 3;

	private static boolean Debug = true;

	private String ip;
	private int port;
	private byte[] mHead;

	protected Socket mSocket;
	SocketDataListener mListener;
	protected InputStream mInput;
	protected OutputStream mOutput;
	private int state;
	
	public TCPSocket(int port, byte[] head) {
		this.port = port;
		mHead = head;
	}


	public TCPSocket(String ip, int port, byte[] head) {
		this.ip = ip;
		this.port = port;
		mHead = head;
	}

//	public static TCPSocket open(String ip, int port) {
//
//		TCPSocket connection = null;
//		try {
//			connection = new TCPSocket(ip, port);
//			connection.mSocket = new Socket(ip, port);
//			System.out.println("Client is created! ip:" + ip + " port:" + port);
//		} catch (UnknownHostException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//
//		return connection;
//	}
	
	protected void onConnected(boolean isSuc) {
		
	}
	
	protected void onDisConnected() {
		
	}
	
	protected void onBeforeConnect() {
		
	}

	public void run() {
		try {
			state = STATE_START;
			onBeforeConnect();
//			InetSocketAddress local = new InetSocketAddress(52792) ;
			mSocket = new Socket(ip, port);//,null, 52792);
			System.out.println("Client is created! ip:" + ip + " port:" + port);
			mInput = mSocket.getInputStream();
			mOutput = mSocket.getOutputStream();
			mSocket.setKeepAlive(true);
			mSocket.setTcpNoDelay(true);
			state = STATE_CONNETTING;
			onConnected(true);
		} catch (Exception e) {
			state = STATE_STOP;
			e.printStackTrace();
			// 连接失败，请检查网络设置
		}
		
		Log.e("test ", "test test");

//		Thread thread = Thread.currentThread();
		int n = 0;
		try {
			while ((n = mInput.read(mHead, 0, mHead.length)) != -1 && isConneting()) {
				if (n > 0) {
					Log.e("test", "n:" + n);
					if (mListener != null) {
						mListener.data(this, mHead, n);
					}
					Arrays.fill(mHead, (byte) 0);
				}
			}
			state = STATE_STOP;
			onDisConnected();
			Log.e("test", "end");
		} catch (Exception e) {
			state = STATE_STOP;
			onDisConnected();
			if (Debug) {
				e.printStackTrace();
				System.out.println("Exception e************ " + e.toString());
			}
		}
	}
	
	public boolean isConneting() {
		return state == STATE_CONNETTING;
	}

	public boolean isRun() {
		return state == STATE_CONNETTING || state == STATE_START;
	}

	public boolean isStop() {
		return state == STATE_STOP;
	}

//	public int readData(byte[] b, int offset, int length) throws IOException {
//		return mInput.read(b, offset, length);
//	}
//
//	public void writeData(byte[] data, int offset, int count)
//			throws IOException {
//		mOutput.write(data, offset, count);
//	}

	public SocketDataListener getSocketDataListener() {
		return mListener;
	}

	public void setSocketDataListener(SocketDataListener listener) {
		mListener = listener;
	}

	public String getIp() {
		return ip;
	}


	public void setIp(String ip) {
		this.ip = ip;
	}
	
	public void close() {
		if (mSocket != null) {
			try {
				mSocket.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				state = STATE_STOP;
				onDisConnected();
			}
		}
	}

}
